import math
from typing import Optional

from cn.edu.cdu.argorithme.commons.list_node import ListNode


class swap_nodes_in_pairs:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        raise NotImplementedError


class Solution(swap_nodes_in_pairs):
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummyHead = ListNode(-1, head)
        prev = dummyHead
        curr = prev.next
        while curr and curr.next:
            _next = curr.next
            curr.next = curr.next.next
            _next.next = curr
            prev.next = _next
            prev = curr
            curr = prev.next
        return dummyHead.next
